<?php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Http\Requests\SmsSeedRequest;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

        $this->middleware('guest');

    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $validator = Validator::make($data, [
            'name' => [
                "required",
                "min:4",
                "max:15",
                "regex:/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/",
                "unique:users"
                ],
            'password' => 'required|string|min:6|confirmed',
            'captcha' => 'required|captcha',
            'mobile' =>[
                "required",
                "regex:/^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[3678]|18\d)\d{8}|170[059]\d{7})$/",
                "unique:users",
            ],
            'codesms' =>'required'
        ], [
            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '请输入正确的验证码',
            'mobile'=>'请输入正确手机号码',
            'codesms.required'=> '手机验证码不能为空',
            'name.regex'=>'用户名不能为纯数字',
            'name.min'=>'用户名不能小于4位',
            'name.max'=>'用户名太长了'

        ]);
        $validator->after(function ($validator) {
          //utf8中文长度不能超过8个汉字
            if (strlen($validator->getData()['name'])>=25) {
                $validator->errors()->add('name', '用户名超过长度');
            }
        });
        $validator->after(function ($validator) {
            if ($validator->getData()['mobile']!=session('SmsTmpMobile') || $validator->getData()['codesms']!=session('SmsTmpCode')) {
                $validator->errors()->add('codesms', '手机号码未通过验证，请重新发送验证码');
            }
        });
        return $validator;
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return App\Models\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'password' => bcrypt($data['password']),
            'mobile' => $data['mobile'],
            'mobilestatus' => true,
            'regdate' => time()
        ]);
    }


    protected function seedRegistrationSms(SmsSeedRequest $data)
    {
        //防刷验证码
        $minute=floor((time()-session('SmsTmpCodeBeginTime'))%86400/60);
        $second=floor((time()-session('SmsTmpCodeBeginTime'))%86400%60);
        if(($minute*60+$second)<30 ){
            return '短信发送间隔过快,请稍后再试';
        }else{

            return seed_sms_code($data["mobile"]);
        }
    }

}
